//
//  ZCEssenceViewController.m
//  百思不得姐
//
//  Created by Ning Xie on 16/8/28.
//  Copyright © 2016年 zhangchong. All rights reserved.
//

#import "ZCEssenceViewController.h"
#import "ZCRecommendTableViewController.h"
#import "ZCEssenceTableViewController.h"




typedef enum : NSInteger {
    AllButton = 0,
    VideoButton = 1,
    VoiceButton = 2,
    PictureButton = 3,
    TextButton = 4
} PostType;

@interface ZCEssenceViewController ()<UIScrollViewDelegate>

@property (strong, nonatomic) UIButton *currentSelectedBtn;
@property (strong, nonatomic) NSMutableArray<UIButton*> *btnArray;

@property (weak, nonatomic) UIView *titleView;//导航栏
@property (weak, nonatomic) UIView *indicatorView;//滚动小条
@property (weak, nonatomic) UIScrollView *scrollView;//滚动视图

@end

@implementation ZCEssenceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupChildControllers];
    [self setupUI];
}

-(NSMutableArray<UIButton *> *)btnArray{
    if (_btnArray == nil) {
        _btnArray = [NSMutableArray array];
    }
    return _btnArray;
}

//MARK:页面布局
-(void)setupUI{
    
    self.navigationItem.titleView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MainTitle"]];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"MainTagSubIcon" selectedImage:@"MainTagSubIconClick" action:@selector(leftBarButtonItemClick) target:self];
    
    //导航视图
    UIView *titleView =[[UIView alloc]init];
    self.titleView = titleView;
    titleView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:titleView];
    titleView.frame = CGRectMake(0, 64, self.view.width, 40);
    
    //按钮控件
    NSArray *btnArray = @[@"全部",@"视频",@"声音",@"图片",@"段子"];
    CGFloat height = 40;
    CGFloat width = [UIScreen mainScreen].bounds.size.width/btnArray.count;
    for (NSInteger i = 0; i < btnArray.count; i++) {
        UIButton *button = [UIButton buttonWithTitle:btnArray[i] textColor:[UIColor blackColor] selectedColor:[UIColor redColor] fontSize:15];
        [button addTarget:self action:@selector(postTypeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(i*width, 0, width, height);
        button.tag = i;
        [titleView addSubview:button];
        [self.btnArray addObject:button];
        
        if ([btnArray[i] isEqualToString:@"全部"]) {
            button.enabled = NO;
            self.currentSelectedBtn = button;

        }
    }
    
    //滚动小条
    UIView *indicatorView =[[UIView alloc]init];
    self.indicatorView = indicatorView;
    indicatorView.backgroundColor = [UIColor redColor];
    [titleView addSubview:indicatorView];
    indicatorView.frame = CGRectMake(0, titleView.height -2, width, 2);

    
    
    //滚动视图
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    self.scrollView = scrollView;
    scrollView.delegate = self;
    scrollView.contentSize = CGSizeMake(self.view.width*btnArray.count, 0);
    scrollView.backgroundColor = ZCGlobalRBG;
    scrollView.pagingEnabled = YES;
    scrollView.bounces = NO;
    [self.view addSubview:scrollView];
    scrollView.frame = self.view.bounds;
    [self.view insertSubview:scrollView atIndex:0];
    
    [self scrollViewDidEndDecelerating:scrollView];
    
}

//添加子控制器
-(void)setupChildControllers{
    
    for (NSInteger i = 0; i < 5; i++) {
        ZCEssenceTableViewController *vc = [[ZCEssenceTableViewController alloc]init];
        vc.tableView.backgroundColor = ZCGlobalRBG;
        [self addChildViewController:vc];
    }
}



//MARK:导航栏视图按钮点击事件
-(void)postTypeButtonAction:(UIButton*)sender{

    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.centerX = sender.centerX;
        self.indicatorView.width = sender.width;
    }];
    self.currentSelectedBtn.enabled = YES;
    sender.enabled = NO;
    self.currentSelectedBtn = sender;
    
    //滚动视图滚动
    [self.scrollView setContentOffset:CGPointMake(self.view.width*sender.tag, 0) animated:YES];
    
    
}

//MARK:左上角推荐按钮
-(void)leftBarButtonItemClick{
    
    ZCRecommendTableViewController *recommendVC = [[ZCRecommendTableViewController alloc]init];
    
    [self.navigationController pushViewController:recommendVC animated:YES];
}

//MARK:UIScrollView代理方法



-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{

    NSInteger index = scrollView.contentOffset.x / self.view.width;
    DataType dataType;
    switch (self.btnArray[index].tag) {
        case AllButton:
            dataType = All;
            break;
        case VideoButton:
            dataType = Video;
            break;
        case VoiceButton:
            dataType = Voice;
            break;
        case PictureButton:
            dataType = Picture;
            break;
        case TextButton:
            dataType = Text;
            break;
        default:
            break;
    }
    
    
    ZCEssenceTableViewController *VC = self.childViewControllers[index];

    VC.dataType = dataType;
    VC.view.x = scrollView.contentOffset.x;
    VC.view.y = 0;
    VC.view.height = scrollView.height;
    VC.tableView.contentInset = UIEdgeInsetsMake(CGRectGetMaxY(self.titleView.frame), 0, self.tabBarController.tabBar.height, 0);
    VC.tableView.scrollIndicatorInsets = VC.tableView.contentInset;
    
    [scrollView addSubview:VC.tableView];
    [self postTypeButtonAction:self.btnArray[index]];
}



-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    [self scrollViewDidEndScrollingAnimation:scrollView];
    
}



@end
